import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpRequest } from '../../../../service/httpRequest';
import { flyIn } from '../../../../animations/fly-in';
import { ModalDirective } from 'ng2-bootstrap/modal';
declare let $:any;
@Component({
  selector: 'app-device-detail',
  templateUrl: './device-detail.component.html',
  styleUrls: ['./device-detail.component.css'],
  animations: [
    flyIn
  ]
})
export class DeviceDetailComponent implements OnInit {
  deviceId:string = '';
  data:any = {xuliehao:'',shiyongdizhi:'',shiyonggongsi:'',weibaorenid:'',weibaoleixing:'',weibaozhouqi:'',
    anzhuangriqi:''};
  mansData:any;
  constructor(private activeRoute:ActivatedRoute,private httpRequest:HttpRequest) { }

  ngOnInit() {
    this.activeRoute.params.subscribe(params=>{
      if(params['deviceId'] == 'none') {
        this.deviceId = '';
      }
      else{
        this.deviceId = params['deviceId'];
      }

      this.getData();
      
    });
    this.initDatePicker();
    this.getServiceMan();
  }

  getData(){
    this.httpRequest.sendGet('device/ext/wb',{
      device_id:this.deviceId.replace('|','/')
    }).subscribe(res=>{

      if(res.result){
        this.data = res.result;       
        this.data.weibaorenid = res.result.weibaoren_id + "," + res.result.mobile;
      }
    });
  }
  getServiceMan():any{
    return this.httpRequest.sendGet('account/serviceman').subscribe(res=>{
      this.mansData = res.result;
      for(var i=0;i<res.result.length;i++){
        this.mansData[i].weibaorenid = res.result[i].id + "," + res.result[i].mobile
      }
    },err=>{});
  }

  initDatePicker(){
    let initDate = new Date();
    let thisDay = initDate.getFullYear()+'-'+((initDate.getMonth()+1) < 10 ? ('0'+(initDate.getMonth()+1)) : (initDate.getMonth()+1)) +'-'+ initDate.getDate();
    let dateOptions = {
      autoclose:true,
      language:"zh-CN",
      format:"yyyy-mm-dd",
      startView:'month',
      minView:'month',   
      initialDate : new Date(),  
      endDate : new Date(),
      todayBtn:true
    };

    $('#datetimepicker1').datetimepicker(dateOptions);
    $('#datetimepicker2').datetimepicker(dateOptions);
  }
  showDatePicker(type){
    $('#datetimepicker'+type).datetimepicker('show');
  }

  save(){

     this.httpRequest.sendPost('device/ext/wb',{
      device_id: this.deviceId.replace('|','/'),
      xuliehao:this.data.xuliehao,
      chuchangriqi : $('#datetimepicker1').val(),
      shiyongdizhi : this.data.shiyongdizhi,
      shiyonggongsi : this.data.shiyonggongsi,
      anzhuangriqi:  $('#datetimepicker2').val(),
      weibaoren_id: this.data.weibaorenid.split(',')[0],
      weibaoleixing: this.data.weibaoleixing,
      weibaozhouqi:  this.data.weibaozhouqi  

    }).subscribe(res=>{
      alert("设备信息已完善！");     
      this.getData();
    },err => {alert('设备信息未完善!');console.log(err)});


  }
}
